﻿using Aspose.Slides;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Test1
{
    public static class ShapeExtension
    {
        public static bool IsTitleShape(this IShape p_shape)
        {
            if (p_shape == null)
            {
                return false;
            }

            var placeholder = p_shape.Placeholder;
            if (placeholder != null)
            {
                switch (placeholder.Type)
                {
                    // Any title shape.
                    case PlaceholderType.Title:
                    // A centered title.
                    case PlaceholderType.CenteredTitle:
                        return true;

                    default:
                        return false;
                }
            }

            return false;
        }
    }
    public class SlideTitle
    {
        public int PageNum { get; set; }

        public int TitleCount { get; set; }

        public string[] Titles { get; set; }
        public override string ToString()
        {
            string title = "";
            for (int i = 0; i < Titles.Length; i++)
            {
                title += Titles[i] + " ";
            }

            return title;
        }
    }
    public static class PresentationExtension
    {
        public static IEnumerable<SlideTitle> GetTitles(this IPresentation p_presentation)
        {
            var presentation = p_presentation;
            if (presentation != null)
            {
                foreach (var slide in presentation.Slides)
                {
                    List<string> titles = new List<string>();

                    foreach (var shape in slide.Shapes)
                    {
                        if (!shape.IsTitleShape())
                        {
                            continue;
                        }

                        var autoShape = shape as AutoShape;
                        if (autoShape == null)
                        {
                            continue;
                        }

                        titles.Add(autoShape.TextFrame.Text);
                    }

                    var title = new SlideTitle()
                    {
                        PageNum = slide.SlideNumber,
                        TitleCount = titles.Count,
                        Titles = titles.ToArray()
                    };

                    yield return title;
                }
            }
        }
    }

    public static class SlidesExtension
    {
        public static string GetTitles(this ISlide p_slide,string defaultTitle)
        {
            List<string> titles = new List<string>();
            foreach (var shape in p_slide.Shapes)
            {
                if (!shape.IsTitleShape())
                {
                    continue;
                }
                var autoShape = shape as AutoShape;
                if (autoShape == null)
                {
                    continue;
                }
                titles.Add(autoShape.TextFrame.Text);
            }
            string s = "";
            foreach (var item in titles)
            {
                s += item + " ";
            }
            if(string.IsNullOrEmpty(s))
            {
                s = defaultTitle;
            }
            return s;
        }
    }
}